﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MensUnoShop.Models;

namespace MensUnoShop.Models.Discount
{
    /// <summary>
    /// 固定判斷之購物金折抵
    /// </summary>
    [Serializable]
    public class CouponDiscount:ITradeInDiscount
    {
        private string _title;
        private int _deprice;
        private int _totalprice;
        private Order _od;
        private Coupon _cp;
        private Config.OrderDiscountType _sktype;
        public string TITLE { set { _title = value; } get { return _title; } }
        public int DEPRICE { set { _deprice = value; } get { return _deprice; } }
        public Config.OrderDiscountType SKTYPE { set { _sktype = value; } get { return _sktype; } }


        public CouponDiscount()
        {
            _title = "網路優惠券折扣";
            _deprice = 0;
            _sktype = Config.OrderDiscountType.CouponDiscount;
        }


        public CouponDiscount(Coupon cp, Order od)
            : this()
        {
            _totalprice = od.TOTALPRICE-od.DSKLIST.Sum(d=>d.DEPRICE);
            _cp = cp;
            _od = od;
        }

        public void GetDiscount()
        {
            if (!_od.MLIST.Any(m => m.TYPE == Config.MarketType.Mangazine))
                _deprice = _cp.PRICE;
            else
                _deprice = 0;
        }

        public bool SetDiscount(string txid)
        {
            DBMember dbm = new DBMember();
            return dbm.SetCouponUsed(_cp.MCID, txid);
        }

        /// <summary>
        /// 交易失敗處理
        /// </summary>
        public void DealFail(string txid)
        {
            DBMember dbm = new DBMember();
            dbm.SetCouponRevert(_cp.MCID, txid);
        }
    }
}
